﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Threading;
using System.Web.Http;

using ZeusLite.Code;

namespace ZeusLite.Controllers
{
  /// <summary>
  /// HU - Mennyiség módosítás
  /// EN - Change picked quantity
  /// </summary>
  public class PickingQuantityController : ApiController
  {
    /// <summary>
    /// 
    /// </summary>
    /// <param name="page">Page name</param>
    /// <param name="lang">Language code</param>
    /// <param name="token">User ID / Access Token</param>
    /// <param name="itemid"></param>
    /// <param name="quantity"></param>
    /// <returns></returns>
    public HttpResponseMessage Get([FromUri] string page, [FromUri] string lang, 
      [FromUri] string token, [FromUri] string itemid, [FromUri] string quantity)
    {
      WebResult res = new WebResult();

      try
      {
        if ((lang != null) && (lang != ""))
          Thread.CurrentThread.CurrentCulture = new CultureInfo(lang);

        using (SqlConn conn = new SqlConn())
        {
          conn.Open(ConfigurationManager.ConnectionStrings["Database"].ConnectionString, lang);

          // SQL command
          string sql = "EXEC spZeusPickingQuantity @Token, @ItemID, @Quantity";

          // Parameters
          ArrayList array = new ArrayList();

          array.Add(new SqlParameter("@Token", token));
          array.Add(new SqlParameter("@ItemID", itemid));
          array.Add(new SqlParameter("@Quantity", quantity));

          conn.Execute(sql, array.ToArray(), true);

          conn.Close();
        }
      }
      catch (Exception ex)
      {
        res.ErrorCode = 1;
        res.ErrorText = ex.Message;
      }

      // JSON formatted result
      var formatter = new JsonMediaTypeFormatter();

      return Request.CreateResponse(HttpStatusCode.OK, res, formatter);
    }
  }
}
